package com.leapp.sk.viewdelegate.album

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R

/**
 * 编辑相册
 * @author  chaohaipeng  on 2018/8/14.
 */
class EditAlbumDelegate:BaseAppDelegate(){
    /**
     * 加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_edit_album
    }

    /**
     * 设置图片
     */
    fun setImage(context: Context,path:String){
        val iv_aea_page_iamge = getView<ImageView>(R.id.iv_aea_page_iamge)
        setImage(context,path, iv_aea_page_iamge)
    }

    /**
     * 设置头像和昵称
     */
    private fun setImage(context: Context, imagePath: String, imageView: ImageView) {
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestOptions.error(R.mipmap.icon_techer_detial)
        val mRequestManager = Glide.with(context)
        mRequestManager?.load(imagePath)
                ?.apply(mRequestOptions)
                ?.into(imageView)
    }

    /**
     * 初始化事件
     */
     fun initEvent(){
        val et_aea_album_name = getView<ContainsEmojiEditText>(R.id.et_aea_album_name)
        val iv_aea_clear = getView<ImageView>(R.id.iv_aea_clear)
        et_aea_album_name.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                if(compareTo==0){
                    iv_aea_clear.visibility= View.INVISIBLE
                }else{
                    iv_aea_clear.visibility= View.VISIBLE
                }
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })

    }


    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_aea= getView<SKTopBar>(R.id.jtb_aea)
        if (!TextUtils.isEmpty(title)){
            jtb_aea.setTitle(title)
        }
        setEvent(jtb_aea)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar:SKTopBar){
        topBar.setRightTVContent(getActivity<AppCompatActivity>().resources.getString(R.string.string_album_delegate_album))
        topBar.setRightTVColor(ContextCompat.getColor(getActivity<AppCompatActivity>(), R.color.color_FF5894))
        topBar.getTv_ljtb_right_back()?.visibility= View.VISIBLE
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()

            }
            override fun rightOnClick() {

            }

        })
    }



}